// pages/user/user.js
import Dialog from '../../miniprogram_npm/weapp/dialog/dialog';
const app = getApp()
Page({
  data: {
    userInfo: app.getGlobalUserInfo(),
    avatarUrl: app.getGlobalUserInfo().avatarUrl,
    userName: app.getGlobalUserInfo().nickName,
  },

  persion_setting() {
    if (this.data.userInfo != "") {
      wx.navigateTo({
        url: '/pages/userSetting/userSetting',
      })
    } else {
      wx.showToast({
        icon: 'error',
        title: '你尚未登录',
        duration: 2000
      })
    }

    /* console.log(this.data.userInfo)
     console.log(this.data.avatarUrl)
     console.log(this.data.userName) */
  },
  setting_star() {
    wx.navigateTo({
      url: '/pages/star/star',
    })
  },
  setting_points() {
    wx.navigateTo({
      url: '/pages/points/points',
    })
  },
  setting_msg() {
    wx.navigateTo({
      url: '/pages/setting_msg/setting_msg',
    })
  },
  setting_feedback() {
    wx.navigateTo({
      url: '/pages/setting_feedback/setting_feedback',
    })
  },
  /**
   * 退出登录操作
   */
  logout() {
    wx.removeStorage({
      key: 'userInfo',
      success() {
        wx.showToast({
          title: '退出成功',
          icon: 'success',
          duration: 2000,
        });
      },
    });
    setTimeout(() => {
      this.setData({
        avatarUrl: null,
        userName: null,
      })
    }, 1000);
  },
  /**
   * 退出登录弹出框
   */
  dialogLogout() {
    //弹出框的操作
    const beforeClose = (action) => new Promise((resolve) => {
      setTimeout(() => {
        if (action === 'confirm') {
          this.logout()
          resolve(true);
        } else {
          // 是否拦截取消操作
          resolve(true);
        }
      }, 1000);
    });
    Dialog.confirm({
      message: "退出登录后，您将无法使用鲤享小程序的资源",
      confirmButtonText: "确认退出",
      cancelButtonText: "取消",
      beforeClose
    });
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getTabBar().init();
    this.setData({
      userInfo: app.getGlobalUserInfo(),
      avatarUrl: app.getGlobalUserInfo().avatarUrl,
      userName: app.getGlobalUserInfo().nickName,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})